/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Eventelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Samplingprotocol;
import entityBeans.Habitat;
import entityBeans.Specimen;
import java.util.ArrayList;
import java.util.List;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class EventelementJpaController {

    public EventelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Eventelement eventelement) {
        if (eventelement.getSpecimenList() == null) {
            eventelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Samplingprotocol samplingprotocol = eventelement.getSamplingprotocol();
            if (samplingprotocol != null) {
                samplingprotocol = em.getReference(samplingprotocol.getClass(), samplingprotocol.getIdsamplingprotocol());
                eventelement.setSamplingprotocol(samplingprotocol);
            }
            Habitat habitat = eventelement.getHabitat();
            if (habitat != null) {
                habitat = em.getReference(habitat.getClass(), habitat.getIdhabitat());
                eventelement.setHabitat(habitat);
            }
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : eventelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            eventelement.setSpecimenList(attachedSpecimenList);
            em.persist(eventelement);
            if (samplingprotocol != null) {
                samplingprotocol.getEventelementList().add(eventelement);
                samplingprotocol = em.merge(samplingprotocol);
            }
            if (habitat != null) {
                habitat.getEventelementList().add(eventelement);
                habitat = em.merge(habitat);
            }
            for (Specimen specimenListSpecimen : eventelement.getSpecimenList()) {
                Eventelement oldEventelementOfSpecimenListSpecimen = specimenListSpecimen.getEventelement();
                specimenListSpecimen.setEventelement(eventelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldEventelementOfSpecimenListSpecimen != null) {
                    oldEventelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldEventelementOfSpecimenListSpecimen = em.merge(oldEventelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Eventelement eventelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Eventelement persistentEventelement = em.find(Eventelement.class, eventelement.getIdeventelement());
            Samplingprotocol samplingprotocolOld = persistentEventelement.getSamplingprotocol();
            Samplingprotocol samplingprotocolNew = eventelement.getSamplingprotocol();
            Habitat habitatOld = persistentEventelement.getHabitat();
            Habitat habitatNew = eventelement.getHabitat();
            List<Specimen> specimenListOld = new ArrayList<Specimen>(); //persistentEventelement.getSpecimenList();
            List<Specimen> specimenListNew = new ArrayList<Specimen>(); //eventelement.getSpecimenList();
            if (samplingprotocolNew != null) {
                samplingprotocolNew = em.getReference(samplingprotocolNew.getClass(), samplingprotocolNew.getIdsamplingprotocol());
                eventelement.setSamplingprotocol(samplingprotocolNew);
            }
            if (habitatNew != null) {
                habitatNew = em.getReference(habitatNew.getClass(), habitatNew.getIdhabitat());
                eventelement.setHabitat(habitatNew);
            }
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            eventelement.setSpecimenList(specimenListNew);
            eventelement = em.merge(eventelement);
            if (samplingprotocolOld != null && !samplingprotocolOld.equals(samplingprotocolNew)) {
                samplingprotocolOld.getEventelementList().remove(eventelement);
                samplingprotocolOld = em.merge(samplingprotocolOld);
            }
            if (samplingprotocolNew != null && !samplingprotocolNew.equals(samplingprotocolOld)) {
                samplingprotocolNew.getEventelementList().add(eventelement);
                samplingprotocolNew = em.merge(samplingprotocolNew);
            }
            if (habitatOld != null && !habitatOld.equals(habitatNew)) {
                habitatOld.getEventelementList().remove(eventelement);
                habitatOld = em.merge(habitatOld);
            }
            if (habitatNew != null && !habitatNew.equals(habitatOld)) {
                habitatNew.getEventelementList().add(eventelement);
                habitatNew = em.merge(habitatNew);
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setEventelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Eventelement oldEventelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getEventelement();
                    specimenListNewSpecimen.setEventelement(eventelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldEventelementOfSpecimenListNewSpecimen != null && !oldEventelementOfSpecimenListNewSpecimen.equals(eventelement)) {
                        oldEventelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldEventelementOfSpecimenListNewSpecimen = em.merge(oldEventelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = eventelement.getIdeventelement();
                if (findEventelement(id) == null) {
                    throw new NonexistentEntityException("The eventelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Eventelement eventelement;
            try {
                eventelement = em.getReference(Eventelement.class, id);
                eventelement.getIdeventelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The eventelement with id " + id + " no longer exists.", enfe);
            }
            Samplingprotocol samplingprotocol = eventelement.getSamplingprotocol();
            if (samplingprotocol != null) {
                samplingprotocol.getEventelementList().remove(eventelement);
                samplingprotocol = em.merge(samplingprotocol);
            }
            Habitat habitat = eventelement.getHabitat();
            if (habitat != null) {
                habitat.getEventelementList().remove(eventelement);
                habitat = em.merge(habitat);
            }
            List<Specimen> specimenList = eventelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setEventelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(eventelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Eventelement> findEventelementEntities() {
        return findEventelementEntities(true, -1, -1);
    }

    public List<Eventelement> findEventelementEntities(int maxResults, int firstResult) {
        return findEventelementEntities(false, maxResults, firstResult);
    }

    private List<Eventelement> findEventelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Eventelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Eventelement findEventelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Eventelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getEventelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Eventelement> rt = cq.from(Eventelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
